//
//  HYLWeiBoCell.m
//  微博－非等高cell的应用
//
//  Created by hyl on 16/5/30.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "HYLWeiBoCell.h"
#import "HYLWeiBo.h"
@interface HYLWeiBoCell ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameLable;
@property (weak, nonatomic) IBOutlet UIImageView *vipView;
@property (weak, nonatomic) IBOutlet UILabel *contentLable;
@property (weak, nonatomic) IBOutlet UIImageView *pictureView;

@end

@implementation HYLWeiBoCell
#pragma mark - 类工厂方法
+(instancetype) weiBoCellWithTableView:(UITableView *)tableView{
    //缓存中是否有
    HYLWeiBoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"weibo"];
    //没有怎么办,不管加载还是什么的送一个给它就可以了
    if (cell==nil) {
        cell=[[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([HYLWeiBoCell class]) owner:nil options:nil]firstObject];
    }
    
    return cell;
}
#pragma mark - setter/getter
-(void) setWeiBo:(HYLWeiBo *)weiBo{
    self.nameLable.text=weiBo.name;
    self.iconView.image=[UIImage imageNamed:weiBo.icon];
    if (weiBo.isVip) {
        self.vipView.hidden=NO;
        self.nameLable.textColor=[UIColor orangeColor];
        self.vipView.image=[UIImage imageNamed:@"vip"];
    }else{
        self.vipView.hidden=YES;
        self.nameLable.textColor=[UIColor blackColor];
    }
    self.contentLable.text=weiBo.text;
    //NSLog(@"%@ %@",self.textLabel.text,weiBo.text);
    
    if (weiBo.picture) {
        self.pictureView.hidden=NO;
        self.pictureView.image=[UIImage imageNamed:weiBo.picture];
    } else {
        self.pictureView.hidden=YES;
    }
    //强制刷新
    [self layoutIfNeeded];
    
    //算每个cell的高度
    if (self.pictureView.hidden) {
        weiBo.cellHeight=CGRectGetMaxY(self.contentLable.frame)+10;
    } else {
        weiBo.cellHeight=CGRectGetMaxY(self.pictureView.frame)+10;
    }
}
#pragma mark -
-(void) awakeFromNib{
    self.contentLable.preferredMaxLayoutWidth=[UIScreen mainScreen].bounds.size.width-20;
}
@end
